#include <iostream>
#include <string>
#include <vector>

class Memento {
    public:
    Memento(std::string state) : state_(state) {}
    std::string GetState() {return state_;}
    private:
    std::string state_;
};

class Originator {
    public:
    void SetState(std::string state) {
        state_ = state;
    }

    std::string GetState() {
        return state_;
    }

    // 创建备忘录
    Memento* CreateMemento() {
        return new Memento(state_);
    }

    // 恢复备忘录
    void RestoreState(Memento* memento) {
        state_ = memento->GetState();
    }

    private:
    std::string state_;
};

class Careker {
    public:
    void AddMemento(Memento* memento) {
        mementos_.push_back(memento);
    }  

    Memento* GetMemento(int index) {
        return mementos_[index];
    }
    private:
    std::vector<Memento*> mementos_;
};

int main () {
    std::cout << "hello memento pattern!!!" << std::endl;
    Originator originator;
    Careker careker;

    originator.SetState("State 1");
    careker.AddMemento(originator.CreateMemento());

    originator.SetState("State 2");
    careker.AddMemento(originator.CreateMemento());

    originator.SetState("State 3");
    careker.AddMemento(originator.CreateMemento());

    originator.RestoreState(careker.GetMemento(1));
    std::cout << originator.GetState() << std::endl;

    return 0;
}